﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class API_A_Userreg : System.Web.UI.Page
{
    /// <summary>
    /// USERSYSTEM LOGON.API 用户系统API
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        string returnStr = string.Empty;
        string editType = Request.Params["edit"];//操作类型
        switch (editType)
        {
            case "add":
                {
                    returnStr = UserAdd();
                    break;
                }
            case "act":
                {
                    returnStr = UserAct();
                    break;
                }
            case "logon":
                {
                    returnStr = UserLogon();
                    break;
                }
            case "list":
                {
                    returnStr = QuestionList();
                    break;
                }
            case "check":
                {
                    returnStr = UserNameCheck();
                    break;
                }
            default: break;
        }
        Response.Write(returnStr);
    }
    /// <summary>
    /// 注册用户
    /// </summary>
    /// <returns></returns>
    protected string UserAdd()
    {
        MODEL.UserLogon model = new MODEL.UserLogon();
        BLL.UserLogon bll = new BLL.UserLogon();
        DateTime regTime=DateTime.Now;
        model.UID = bll.GetMaxId();
        model.UlogonIP = Request.Form["i"];
        model.UlogonName = Request.Form["u"];
        model.UlogonTime = regTime;
        model.UpassWord = EncrypterMD5.MD5.Md5(Request.Form["p"].ToString(), 32);
        model.UprevIP = Request.Form["i"];
        model.UprevTime = regTime;
        model.Uregtime = regTime;
        model.Ustate = false;

        string returnStr = string.Empty;
        string keyStr = DBUtility.DbHelper.Encrypt(model.UlogonIP);
        bool flag = bll.UserLogonAdd(model);
        if (flag)
        {
            flag = UserPassSafeGuard(model.UID);
            if (Session["spreader"] != "" && Session["spreader"] != null)
            {
                SpreadInsert(model, Session["spreader"].ToString());
            }
        }
        else
        {
            flag = false;
        }
        returnStr = "{\"id\":\""+model.UID+"\",\"key\":\"" + keyStr + "\",\"err\":\"" + flag.ToString().ToLower() + "\"}";
        return returnStr;
    }
    /// <summary>
    /// 用户登录验证
    /// </summary>
    /// <returns></returns>
    protected string UserLogon()
    {
        MODEL.UserLogon model = new MODEL.UserLogon();
        BLL.UserLogon bll = new BLL.UserLogon();
        model.UlogonName = Request.Form["n"];
        model.UlogonIP = Request.Form["i"];
        model.UpassWord = EncrypterMD5.MD5.Md5(Request.Form["p"].ToString(), 32);
        DataSet ds = bll.UserLogonList(model.UlogonName);
        string errNum = string.Empty;
        string returnStr = string.Empty;
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Ustate"].ToString() == null || ds.Tables[0].Rows[0]["Ustate"].ToString() == "")
                {
                    errNum = "0";
                }
                else
                {
                    if (ds.Tables[0].Rows[0]["Ustate"].ToString() == "0" || ds.Tables[0].Rows[0]["Ustate"].ToString().ToLower() == "false")
                    {
                        errNum = "1";
                    }
                    else
                    {
                        bool flag = bll.UserLogonCheck(model);
                        if (flag)
                        {
                                errNum = "OK";
                                Session["username"] = model.UlogonName;
                                DataSet userDs = bll.UserLogonListByName(model.UlogonName);
                                Session["userid"] = userDs.Tables[0].Rows[0]["Uid"].ToString();
                        }
                        else
                        {
                            errNum = "2";
                        }
                    }
                }
            }
            else
            {
                errNum = "0";
            }
        }
        else
        {
            errNum = "0";
        }
        returnStr = "{\"err\":\"" + errNum + "\"}";
        return returnStr;
    }
    /// <summary>
    /// 用户激活
    /// </summary>
    /// <returns></returns>
    protected string UserAct()
    {
        string logonIp = DBUtility.DbHelper.Decrypt(Request.Params["key"]);
        string userId = Request.Params["id"];
        BLL.UserLogon bll = new BLL.UserLogon();
        return bll.UserLogonUpdateState(userId, logonIp).ToString().ToLower();
    }
    /// <summary>
    /// 用户密保列表
    /// </summary>
    /// <returns></returns>
    protected string QuestionList()
    {
        BLL.UserQuestion bll = new BLL.UserQuestion();
        DataSet ds = bll.UserQuestionList(true);
        StringBuilder htmlStr = new StringBuilder();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            htmlStr.Append("<option value=\"" + ds.Tables[0].Rows[i]["Uid"].ToString() + "\">" + ds.Tables[0].Rows[i]["Uquestion"].ToString() + "</option>");
        }
        return htmlStr.ToString();
    }
    /// <summary>
    /// 密码保护设置
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    protected bool UserPassSafeGuard(string userId)
    {
        MODEL.UserPassSafeguard model = new MODEL.UserPassSafeguard();
        BLL.UserPassSafeGuard bll = new BLL.UserPassSafeGuard();
        model.UID = userId;
        model.Uanswer = Request.Form["a"];
        model.UquestionID = Convert.ToInt32(Request.Form["q"]);

        return bll.UserPassSafeGuardAdd(model);
    }
    /// <summary>
    /// 检查用户名是否可用
    /// </summary>
    /// <returns></returns>
    protected string UserNameCheck()
    {
        string userName = Request.Form["u"];
        string returnStr = string.Empty;
        BLL.UserLogon bll = new BLL.UserLogon();
        DataSet ds = bll.UserNameCheck(userName);
        int nameCount = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
        if (nameCount > 0)
        {
            returnStr = "false";
        }
        else
        {
            returnStr = "true";
        }
        return returnStr;
    }
    protected void SpreadInsert(MODEL.UserLogon model, string spreadName)
    {
        DateTime nowTime=DateTime.Now;
        BLL.Spreader spBll = new BLL.Spreader();
        MODEL.SpreadDeputy spModel = new MODEL.SpreadDeputy();
        BLL.SpreadDeputy bll = new BLL.SpreadDeputy();
        bool flag = false;
        spModel.SSPEARDID = spBll.GetSpreadIdByName(spreadName);
        spModel.SUSERID = model.UID;
        spModel.SNEWCARDID = "";
        spModel.SIP = model.UlogonIP;
        spModel.STIME = nowTime;
        spModel.SDAY = nowTime.Day.ToString();
        spModel.SMONTH = nowTime.Month.ToString();
        spModel.SYEAR = nowTime.Year.ToString();
        spModel.STYPE = true;

        flag = bll.SpreadDeputyAdd(spModel);
        if (flag)
        {
            bll.SpreadMasterUpdate(spModel);
        }
    }
}
